import {parish} from './parish.js';
import {dioceseMarker} from './dioceseMarker.js';
//地图实例
let _map = null;
//行政区划实例
let _districtExplorer = null
//当前选中行政区
let current_feature = null;
let show_diocese = null;
let province_view = null;
export const district = (map) => {
    _map = map;
    AMapUI.loadUI(['geo/DistrictExplorer'], function (DistrictExplorer) {
        //创建一个实例
        _districtExplorer = new DistrictExplorer({
            map: map, //关联的地图实例
            eventSupport: true //事件支持
        });
        //绘制行政区划
        load_area_node();
        //执行行政区划事件
        district_event();
        map.on('zoomend',()=>{
            if(!show_diocese) return;
            if(map.getZoom()<=province_view-1){
                showDiocese(current_feature.properties.adcode);
                show_diocese = null;
            }
        })
    })
}

//行正区划绘制(默认绘制全国)
const load_area_node = (adcode = 100000) => {
    _districtExplorer.loadAreaNode(adcode, function (error, areaNode) {
        if (error) {
            console.error(error);
            return;
        }
        // _districtExplorer.setAreaNodesForLocating([areaNode]);
        _districtExplorer.setHoverFeature(null);
        //绘制载入的区划节点
        renderAreaNode(areaNode);
    });

}

//绘制载入的区划节点
const renderAreaNode = (areaNode) => {
    //更新地图视野
    if(areaNode.adcode>100000){
    // _map.setBounds(areaNode.getBounds());

    }
    //清除已有的绘制内容
    _districtExplorer.clearFeaturePolygons();
    //just some colors
    let colors = ["#3366cc", "#dc3912", "#ff9900", "#109618", "#990099", "#0099c6", "#dd4477", "#66aa00"];
    //绘制子级区划
    _districtExplorer.renderSubFeatures(areaNode, function (feature, i) {
        let fillColor = colors[i % colors.length];
        let strokeColor = colors[colors.length - 1 - i % colors.length];
        return {
            cursor: 'default',
            bubble: false,
            strokeColor: strokeColor, //线颜色
            strokeOpacity: 1, //线透明度
            strokeWeight: 1, //线宽
            fillColor: fillColor, //填充色
            fillOpacity: 0.2, //填充透明度
        };
    });

    //绘制父级区划，仅用黑色描边
    _districtExplorer.renderParentFeature(areaNode, {
        cursor: 'pointer',
        bubble: true,
        strokeColor: 'black', //线颜色
        fillColor: null,
        strokeWeight: 1, //线宽
    })
    // 更新地图视野以适合区划面
    if(areaNode.adcode>100000){
        _map.setFitView(_districtExplorer.getAllFeaturePolygons());
        setTimeout(()=>{
            province_view = _map.getZoom();
        },2000)
    }
}

//行政区划事件
const district_event = () => {
    var textMark = new AMap.Text({
        anchor: 'bottom-left',
        map: _map,
        visible: false,
        bubble: true,
        text: '文本标记',
        style: {},
    });

    //鼠标移入移出
    _districtExplorer.on('featureMouseout featureMouseover', function (e, feature) {
        // debugger;
        let isHover = e.type === 'featureMouseover';
        //更改选中区域透明度
        let polys = _districtExplorer.findFeaturePolygonsByAdcode(feature.properties.adcode);
        for (let item of polys) {
            item.setOptions({
                fillOpacity: isHover ? 0.5 : 0.2
            })
        }

        if (!isHover) {
            textMark.hide();
            return;
        }
        textMark.setText(feature.properties.name);
        textMark.setPosition(e.originalEvent.lnglat);
        textMark.show();
    });

    _districtExplorer.on('featureMousemove', function (e, feature) {
        textMark.setPosition(e.originalEvent.lnglat);
    })

    _districtExplorer.on('featureClick', function (e, feature) {
        //重复点击验证
        if (current_feature == feature) {
            return;
        }
        current_feature = feature;
        //省级区划添加覆盖物
        if (feature.properties.level == 'province') {
            //marker(_map, feature,true);
            show_diocese = null;
            showDiocese(feature.properties.adcode);
            // dioceseMarker(_map,feature.properties.adcode,(event,diocese)=>{
            //    _map.clearMap();
            //    parish(_map,diocese);
            // });
        }
        //绘制子级行政区划
        load_area_node(feature.properties.adcode);
    })
    //外部区域被点击
    _districtExplorer.on('outsideClick', function (e, feature) {
        // console.log(feature);
        _districtExplorer.locatePosition(e.originalEvent.lnglat, function (error, routeFeatures) {
            if (routeFeatures && routeFeatures.length > 1) {
                //切换到省级区域
                load_area_node(routeFeatures[1].properties.adcode);
            } else {
                _map.remove(_map.getAllOverlays('marker'));
                _map.remove(_map.getAllOverlays('circle'));
                _map.remove(_map.getAllOverlays('text'));
                _map.clearInfoWindow();
                //切换到全国
                load_area_node();
            }

        }, {
            levelLimit: 2
        });
    });
}

const showDiocese = (adcode)=>{
    dioceseMarker(_map,adcode,(event,diocese)=>{
        show_diocese = true;
        parish(_map,diocese);
    });
}